<?php
header('Content-Type: text/plain;');

$private_template	= preg_split('/\W/', "UV_HANDLE_TYPE_PRIVATE
UV_REQ_TYPE_PRIVATE
UV_REQ_PRIVATE_FIELDS
UV_STREAM_PRIVATE_FIELDS
UV_TCP_PRIVATE_FIELDS
UV_PREPARE_PRIVATE_FIELDS
UV_CHECK_PRIVATE_FIELDS
UV_IDLE_PRIVATE_FIELDS
UV_ASYNC_PRIVATE_FIELDS
UV_TIMER_PRIVATE_FIELDS
UV_GETADDRINFO_PRIVATE_FIELDS
UV_FS_REQ_PRIVATE_FIELDS
UV_WORK_PRIVATE_FIELDS
UV_FS_EVENT_PRIVATE_FIELDS
UV_SIGNAL_PRIVATE_FIELDS
UV_LOOP_PRIVATE_FIELDS
UV_LOOP_PRIVATE_PLATFORM_FIELDS" ) ;

function parse($f, array & $all, array & $map ){
    $data   = file_get_contents($f);
    $data   = preg_replace('/\/\*.+?\*\//ms', '', $data );
  
    preg_match_all('/\Wstruct\s+(\w+)/', $data, $ls);
    if( empty($ls[0]) ) {
        return ;
    }
    $a = array_unique( $ls[1] );
    if( empty($map ) ) {
        $map    = $a ;
        $all[$f] = array_values($a) ;
        return ;
    } 
    $_a = array() ;
    foreach($a as $s ) {
        if( !in_array($s, $map) ) {
            $_a[]   = $s ;
        }
    }
    $all[$f] = $_a ;
}

$files = array_merge(glob("include/*.h"), glob("include/*/uv-*.h") );

$map    = array() ;
$all    = array() ;
foreach($files  as $f){
    parse($f, $all , $map ) ;
}
unset($all['include/uv-private/uv-unix.h']);
$_all   = array();
foreach($all as $o) {
    $_all = array_merge($_all, $o);
}

$len    = count($_all) + count($private_template) ;
?>
#include "uv.h"

<?php

foreach($private_template as $template){
  echo "struct {$template}__ { $template };\n";   
}
    
?>

typedef struct uv_struct_info_s {
    char* name;
    int  size;
} uv_struct_info ;

UV_EXTERN void* get_uv_struct_info(){
    static uv_struct_info uv_struct_info_list[ <?php echo $len;?> ] = {
<?php
foreach($private_template as $struct) {
    $struct = $struct . '__' ;
    echo "\t{\"{$struct}\", sizeof(struct {$struct})},\n";
}
foreach($_all as $struct) {
    echo "\t{\"{$struct}\", sizeof(struct {$struct})},\n";
}
?>
    };
	static uv_struct_info list  = { NULL, <?php echo $len;?> };
	list.name = (char*) uv_struct_info_list ;
	return  & list ; 
}